import Qs from 'qs';
import { stringify } from '@/assets/js/utils';

export default function (app) {
    let fetch = app.$axios;
    // 基本配置
    fetch.defaults.baseURL = "http://www.ys.com/index.php";
    fetch.defaults.timeout = 10000;
    fetch.defaults.crossDomain = true;
    fetch.defaults.withCredentials = true;
    fetch.defaults.headers["Content-Type"] = "application/json";
    fetch.defaults.validateStatus = function (status) {
        return status;
    }
    // 数据预处理
    fetch.defaults.transformRequest = [function (data, headers) {
        let req = '';
        if (headers['Content-Type'] == 'application/x-www-form-urlencoded' && typeof data == 'object' && Object.prototype.toString.call(data) != '[object FormData]') {
            req = Qs.stringify(data);
        } else if (headers['Content-Type'] == 'application/x-www-form-urlencoded' && typeof data == 'string') {
            try {
                let obj = JSON.parse(data);
                req = stringify(obj);
            } catch (error) {
                req = data;
            }
        } else if (typeof data == 'object' && Object.prototype.toString.call(data) != '[object FormData]') {
            req = JSON.stringify(data)
        } else {
            req = data;
        }
        return req;
        // 对 data 进行任意转换处理
    }];
    // 请求回调
    fetch.onRequest(config => {
        // 接口 是否判断错误 提示
        if (config.params && config.params._MessageTip) {
            delete config.params._MessageTip;
            config._MessageTip = true;
        }
        if (config.data && config.data._MessageTip) {
            delete config.data._MessageTip;
            config._MessageTip = true;
        }
        // 接口是否判断 非200 提示
        if (config.params && config.params._MessageCodeTip) {
            delete config.params._MessageCodeTip;
            config._MessageCodeTip = true;
        }
        if (config.data && config.data._MessageCodeTip) {
            delete config.data._MessageCodeTip;
            config._MessageCodeTip = true;
        }

        // config.headers['tk'] = "2";
        if (app.store.getters.getToken) {
            // config.headers['tk'] = app.store.getters.getToken;
            config.headers['X-CSRF-TOKEN'] = app.store.getters.getToken;
        }
        return config
    })

    // 返回回调
    fetch.onResponse(data => {
        // 接口是否判断 非200 提示
        if (data.config._MessageCodeTip) {
            try {
                return data.data;
            } catch (error) {
                return null
            }
        }
        if (data.status != 200) {
            let error = '';
            switch (data.status) {
                case 502:
                    error = '连接不上后台服务器';
                    break;
                case 500:
                    error = '服务器内部错误';
                    break
                case 404:
                    error = '未找到远程服务器';
                    break
                case 400:
                    error = '请求发送数据异常';
                    break
                default:
                    error = data.statusText;
            }
            return Promise.reject(new Error(`${error},请求code:${data.status}`))
        }
        if (data.data && typeof data.data == 'object') {
            // 扩展 如果传参中有_MessageTip为true 的话 则不进行data 数据格式校验
            if (data.data.code == -1) {
                app.redirect('/admin/login');
            }
            if (data.data.code) {
                return Promise.reject(data.data)
            }
            return data.data;
        }
        return Promise.reject(data.data)
    })

    // 错误回调
    fetch.onError(error => { })
}